package com.ruiaa.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruiaa.meeting.common.BusinessException;
import com.ruiaa.meeting.common.CommonResponse;
import com.ruiaa.meeting.common.Constant;
import com.ruiaa.meeting.common.ResponseCode;
import com.ruiaa.meeting.model.Api;
import com.ruiaa.meeting.model.MeetingRoom;
import com.ruiaa.meeting.persistence.MeetingRoomMapper;
import com.ruiaa.meeting.service.ApiService;
import com.ruiaa.meeting.service.MeetingRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author chenhuaiyi
 * @date 2022-11-05 15:31
 */
@Service
public class MeetingRoomServiceImpl implements MeetingRoomService {

    @Autowired
    private MeetingRoomMapper meetingRoomMapper;
    @Autowired
    private ApiService apiService;

    @Override
    public CommonResponse<MeetingRoom> openMeetingRoom(Integer roomId) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        MeetingRoom meetingRoom = meetingRoomMapper.selectOne(queryWrapper);
        if (meetingRoom != null) {
            meetingRoom.setState(Constant.ROOM_STATE.OPEN);

            UpdateWrapper<MeetingRoom> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("room_id", meetingRoom.getRoomId());
            updateWrapper.set("state", meetingRoom.getState());

            int rows = meetingRoomMapper.update(meetingRoom, updateWrapper);
            if (rows > 0) {
                return CommonResponse.createForSuccess(meetingRoom);
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "会议室不存在");
        return CommonResponse.createForError("开放会议室失败");
    }

    @Override
    public CommonResponse<MeetingRoom> closeMeetingRoom(Integer roomId) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        MeetingRoom meetingRoom = meetingRoomMapper.selectOne(queryWrapper);
        if (meetingRoom != null) {
            meetingRoom.setState(Constant.ROOM_STATE.CLOSE);

            UpdateWrapper<MeetingRoom> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("room_id", meetingRoom.getRoomId());
            updateWrapper.set("state", meetingRoom.getState());

            int rows = meetingRoomMapper.update(meetingRoom, updateWrapper);
            if (rows > 0) {
                return CommonResponse.createForSuccess(meetingRoom);
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "会议室不存在");
        return CommonResponse.createForError("关闭会议室失败");
    }

    @Override
    public CommonResponse<List<MeetingRoom>> getAllMeetingRooms() {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        List<MeetingRoom> meetingRoomList = meetingRoomMapper.selectList(queryWrapper);
        if (meetingRoomList != null) {
            List<Api> relationalApis = apiService.getRelationalApis(25);
            return CommonResponse.createForSuccessIncludeApis(meetingRoomList, relationalApis);
        }
        return CommonResponse.createForError("查询失败");
    }

    @Override
    public CommonResponse<MeetingRoom> getMeetingRoomById(Integer roomId) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        MeetingRoom meetingRoom = meetingRoomMapper.selectOne(queryWrapper);
        if (meetingRoom != null) {
            return CommonResponse.createForSuccess(meetingRoom);
        }
        return CommonResponse.createForError("会议室不存在");
    }

    @Override
    public CommonResponse<Object> addMeetingRoom(MeetingRoom meetingRoom) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        List<MeetingRoom> meetingRoomList = meetingRoomMapper.selectList(queryWrapper);

        for (MeetingRoom meetingRoom1 : meetingRoomList){
            if (Objects.equals(meetingRoom1.getRoomId(), meetingRoom.getRoomId())){
                throw new BusinessException(ResponseCode.ERROR.getCode(), "该会议室号已存在");
            }
        }

        int rows=meetingRoomMapper.insert(meetingRoom);
        if (rows>0) {
            List<Api> relationalApis = apiService.getRelationalApis(27);
            return CommonResponse.createForSuccessIncludeApis("已添加会议室", relationalApis);
        }

        return CommonResponse.createForError("添加会议室失败");
    }

    @Override
    public CommonResponse<Object> deleteMeetingRoomById(Integer roomId) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        MeetingRoom meetingRoom = meetingRoomMapper.selectOne(queryWrapper);
        if (meetingRoom != null) {

            meetingRoomMapper.delete(queryWrapper);
            return CommonResponse.createForSuccess("删除会议室成功");

        }
        return CommonResponse.createForError("会议室不存在，删除会议室失败");
    }

    @Override
    public CommonResponse<MeetingRoom> updateMeetingRoom(Integer roomId, Integer state, Integer number, String roomName) {
        QueryWrapper<MeetingRoom> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("room_id", roomId);
        MeetingRoom meetingRoom = meetingRoomMapper.selectOne(queryWrapper);
        if (meetingRoom != null) {

            meetingRoom.setState(state);
            meetingRoom.setNumber(number);
            meetingRoom.setRoomName(roomName);

            int rows = meetingRoomMapper.updateById(meetingRoom);
            if (rows > 0) {
                return CommonResponse.createForSuccess(meetingRoom);
            }

        } else throw new BusinessException(ResponseCode.ERROR.getCode(), "会议室不存在");
        return CommonResponse.createForError("更新会议室失败");
    }
}
